<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>输入输出运行实例 | Leon&#39;s blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言这是个我自己期末复习的复习专栏，目的是为了能在各种设备上方便的阅读，接下来是正文：">
<meta property="og:type" content="article">
<meta property="og:title" content="输入输出运行实例">
<meta property="og:url" content="https://leonlee233.github.io/2020/05/25/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E8%BF%90%E8%A1%8C%E5%AE%9E%E4%BE%8B/index.html">
<meta property="og:site_name" content="Leon&#39;s blog">
<meta property="og:description" content="前言这是个我自己期末复习的复习专栏，目的是为了能在各种设备上方便的阅读，接下来是正文：">
<meta property="og:locale">
<meta property="article:published_time" content="2020-05-25T01:15:01.000Z">
<meta property="article:modified_time" content="2023-10-26T05:46:54.418Z">
<meta property="article:author" content="Leon Lee">
<meta property="article:tag" content="学习心得">
<meta property="article:tag" content="Hide">
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="@https:&#x2F;&#x2F;twitter.com&#x2F;leonwithleo">
  
    <link rel="alternate" href="/atom.xml" title="Leon's blog" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Leon&#39;s blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">主页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Suche"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://leonlee233.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-输入输出运行实例" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/05/25/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E8%BF%90%E8%A1%8C%E5%AE%9E%E4%BE%8B/" class="article-date">
  <time class="dt-published" datetime="2020-05-25T01:15:01.000Z" itemprop="datePublished">2020-05-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      输入输出运行实例
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>这是个我自己期末复习的复习专栏，目的是为了能在各种设备上方便的阅读，接下来是正文：<br> <span id="more"></span> </p>
<h4 id="一．write、read函数程序实例"><a href="#一．write、read函数程序实例" class="headerlink" title="一．write、read函数程序实例"></a>一．write、read函数程序实例</h4><p><strong>以下程序首先将3名学生的学号、姓名和成绩写入二进制文件</strong><br><strong>studentinfo.dat中，然后将该文件中的数据读到结构体数组中，并将其输出到屏幕上</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line"><span class="comment">#include&lt;fstream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">struct Student // Student结构体类型定义</span><br><span class="line">&#123;</span><br><span class="line"> char num[10];</span><br><span class="line"> char name[20];</span><br><span class="line"> int score;</span><br><span class="line">&#125;;</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line"> Student stu[3] = &#123; // 定义Student结构体数组并初始化</span><br><span class="line"> &#123;<span class="string">&quot;1210101&quot;</span>, <span class="string">&quot;张三&quot;</span>, 618&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210102&quot;</span>, <span class="string">&quot;李四&quot;</span>, 625&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210103&quot;</span>, <span class="string">&quot;王五&quot;</span>, 612&#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> int i;</span><br><span class="line"> fstream outfile(<span class="string">&quot;studentinfo.dat&quot;</span>, ios::binary|ios::out);</span><br><span class="line"> // 写方式打开</span><br><span class="line"> <span class="keyword">if</span> (!outfile.is_open()) // 判断文件打开操作是否成功</span><br><span class="line"> &#123;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;以写方式打开二进制文件studentinfo.dat失败！&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string"> return 0;</span></span><br><span class="line"><span class="string"> &#125;</span></span><br><span class="line"><span class="string"> for (i=0; i&lt;3; i++) // 将第i名学生的数据写入文件</span></span><br><span class="line"><span class="string"> outfile.write((char*)&amp;stu[i], sizeof(Student)); </span></span><br><span class="line"><span class="string"> outfile.close(); // 关闭文件</span></span><br><span class="line"><span class="string"> fstream infile(&quot;studentinfo.dat&quot;, ios::binary|ios::in); // 读方式打开</span></span><br><span class="line"><span class="string"> if (!infile.is_open()) // 判断文件打开操作是否成功</span></span><br><span class="line"><span class="string"> &#123;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;以读方式打开二进制文件studentinfo.dat失败！&quot;&lt;&lt;endl</span>;</span><br><span class="line"> <span class="built_in">return</span> 0;</span><br><span class="line"> &#125;</span><br><span class="line"> // 将3名学生数据一次性读入到内存中</span><br><span class="line"> infile.read((char*)stu, sizeof(stu)); </span><br><span class="line"> <span class="keyword">for</span> (i=0; i&lt;3; i++)</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;第&quot;</span>&lt;&lt;<span class="string">i+1&lt;&lt;&quot;名学生的学号、姓名和成绩：&quot;</span></span><br><span class="line"><span class="string"> &lt;&lt;stu[i</span>].num&lt;&lt;<span class="string">&quot;,&quot;</span>&lt;&lt;<span class="string">stu[i].name&lt;&lt;&quot;,&quot;&lt;&lt;stu</span>[i].score&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string"> infile.close(); // 关闭文件</span></span><br><span class="line"><span class="string"> return 0;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">可以将程序中：</span></span><br><span class="line"><span class="string">for (i=0; i&lt;3; i++)</span></span><br><span class="line"><span class="string"> outfile.write((char*)&amp;stu[i], sizeof(Student));</span></span><br><span class="line"><span class="string">改为</span></span><br><span class="line"><span class="string"> outfile.write((char*)stu, sizeof(stu)); // 即将3名学生的数据一次性写入文件</span></span><br><span class="line"><span class="string">也可以将程序中：</span></span><br><span class="line"><span class="string"> infile.read((char*)stu, sizeof(stu));</span></span><br><span class="line"><span class="string">改为</span></span><br><span class="line"><span class="string"> for (i=0; i&lt;3; i++) // 将学生数据分3次读入内存中</span></span><br><span class="line"><span class="string"> outfile.read((char*)&amp;stu[i], sizeof(Student));</span></span><br><span class="line"><span class="string"></span></span><br></pre></td></tr></table></figure>

<h4 id="二．自定义类型数据的标准输入输出和文本输入输出"><a href="#二．自定义类型数据的标准输入输出和文本输入输出" class="headerlink" title="二．自定义类型数据的标准输入输出和文本输入输出"></a>二．自定义类型数据的标准输入输出和文本输入输出</h4><p><strong>“&gt;&gt;”和“&lt;&lt;”不能对用户自定义类型的数据直接进行输入&#x2F;输出。要使这两个运算符能够支持自定义类型数据的输入&#x2F;输出，必须由编程人员对它们进行重载。重载的函数形式为：</strong></p>
<p><em><strong>istream&amp; operator&gt;&gt;(istream&amp;, 自定义类型&amp;);</strong></em><br><em><strong>ostream&amp; operator&lt;&lt;(ostream&amp;, 自定义类型&amp;);</strong></em>  </p>
<p><strong>重载提取运算符“&gt;&gt;”和插入运算符“&lt;&lt;”，实现学生结构体变量的输入&#x2F;输出操作</strong>  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line"><span class="comment">#include&lt;fstream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">struct Student // Student结构体类型定义</span><br><span class="line">&#123;</span><br><span class="line"> char num[10];</span><br><span class="line"> char name[20];</span><br><span class="line"> int score;</span><br><span class="line">&#125;;</span><br><span class="line">// “&gt;&gt;”运算符重载函数</span><br><span class="line">istream&amp; operator&gt;&gt;(istream&amp; <span class="keyword">in</span>, </span><br><span class="line">Student&amp; stu)</span><br><span class="line">&#123;</span><br><span class="line"> <span class="keyword">in</span>&gt;&gt;stu.num&gt;&gt;stu.name&gt;&gt;stu.score;</span><br><span class="line"> <span class="built_in">return</span> <span class="keyword">in</span>;</span><br><span class="line">&#125;</span><br><span class="line">// “&lt;&lt;”运算符重载函数</span><br><span class="line">ostream&amp; operator&lt;&lt;(ostream&amp; out, </span><br><span class="line">Student&amp; stu)</span><br><span class="line">&#123;</span><br><span class="line"> out&lt;&lt;<span class="string">stu.num&lt;&lt;&#x27; &#x27;&lt;&lt;stu</span>.name</span><br><span class="line"> &lt;&lt;<span class="string">&#x27; &#x27;</span>&lt;&lt;<span class="string">stu.score&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> return out;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">int main()</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string"> // 定义Student结构体数组并初始化</span></span><br><span class="line"><span class="string"> Student stu</span>[3] = &#123;</span><br><span class="line"> &#123;<span class="string">&quot;1210101&quot;</span>, <span class="string">&quot;张三&quot;</span>, 618&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210102&quot;</span>, <span class="string">&quot;李四&quot;</span>, 625&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210103&quot;</span>, <span class="string">&quot;王五&quot;</span>, 612&#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> int i;</span><br><span class="line"> fstream outfile(<span class="string">&quot;studentinfo.txt&quot;</span>, ios::out);// 以写方式打开文本文件</span><br><span class="line"> <span class="keyword">if</span> (!outfile.is_open()) // 判断文件打开操作是否成功</span><br><span class="line"> &#123;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;以写方式打开文本文件studentinfo.txt失败！&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string"> return 0;</span></span><br><span class="line"><span class="string"> &#125;</span></span><br><span class="line"><span class="string"> // 将第i名学生的数据写入文件，相当于调用outfile.operator&lt;&lt;(stu[i]);</span></span><br><span class="line"><span class="string"> for (i=0; i&lt;3; i++)</span></span><br><span class="line"><span class="string"> outfile&lt;&lt;stu[i];</span></span><br><span class="line"><span class="string"> outfile.close(); // 关闭文件</span></span><br><span class="line"><span class="string"> fstream infile(&quot;studentinfo.txt&quot;, ios::in); // 以读方式打开文本文件</span></span><br><span class="line"><span class="string"> if (!infile.is_open()) // 判断文件打开操作是否成功</span></span><br><span class="line"><span class="string"> &#123;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;以读方式打开文本文件studentinfo.txt失败！&quot;&lt;&lt;endl</span>;</span><br><span class="line"> <span class="built_in">return</span> 0;</span><br><span class="line"> &#125;</span><br><span class="line"> // 将第i名学生的数据读入到内存中，相当于调用infile.operator&gt;&gt;(stu[i]);</span><br><span class="line"> <span class="keyword">for</span> (i=0; i&lt;3; i++)</span><br><span class="line"> infile&gt;&gt;stu[i];</span><br><span class="line"> <span class="keyword">for</span> (i=0; i&lt;3; i++)</span><br><span class="line"> &#123;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;第&quot;</span>&lt;&lt;<span class="string">i+1&lt;&lt;&quot;名学生的学号、姓名和成绩：&quot;;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;stu[i</span>]; // 相当于调用cout.operator&lt;&lt;(stu[i]);</span><br><span class="line"> &#125;</span><br><span class="line"> infile.close(); // 关闭文件</span><br><span class="line"> <span class="built_in">return</span> 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="三．自定义类型数据的二进制文件输入输出"><a href="#三．自定义类型数据的二进制文件输入输出" class="headerlink" title="三．自定义类型数据的二进制文件输入输出"></a>三．自定义类型数据的二进制文件输入输出</h4><p><strong>修改例4-16，使用二进制文件存储学生信息。</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#include&lt;iostream&gt;</span></span><br><span class="line"><span class="comment">#include&lt;fstream&gt;</span></span><br><span class="line">using namespace std;</span><br><span class="line">struct Student // Student结构体类型定义</span><br><span class="line">&#123;</span><br><span class="line"> char num[10];</span><br><span class="line"> char name[20];</span><br><span class="line"> int score;</span><br><span class="line">&#125;;</span><br><span class="line">// “&gt;&gt;”运算符重载函数</span><br><span class="line">istream&amp; operator&gt;&gt;(istream&amp; <span class="keyword">in</span>, </span><br><span class="line">Student&amp; stu)</span><br><span class="line">&#123;</span><br><span class="line"> <span class="keyword">in</span>&gt;&gt;stu.num&gt;&gt;stu.name&gt;&gt;stu.score;</span><br><span class="line"> <span class="built_in">return</span> <span class="keyword">in</span>;</span><br><span class="line">&#125;</span><br><span class="line">// “&lt;&lt;”运算符重载函数</span><br><span class="line">ostream&amp; operator&lt;&lt;(ostream&amp; out, </span><br><span class="line">Student&amp; stu)</span><br><span class="line">&#123;</span><br><span class="line"> out&lt;&lt;<span class="string">stu.num&lt;&lt;&#x27; &#x27;&lt;&lt;stu</span>.name&lt;&lt;<span class="string">&#x27; &#x27;</span></span><br><span class="line"> &lt;&lt;<span class="string">stu.score&lt;&lt;endl;</span></span><br><span class="line"><span class="string"> return out;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">// 文件流的&gt;&gt;运算符重载函数</span></span><br><span class="line"><span class="string">fstream&amp; operator&gt;&gt;(fstream&amp; infile, </span></span><br><span class="line"><span class="string">Student&amp; stu</span>)</span><br><span class="line">&#123;</span><br><span class="line"> infile.read((char*)&amp;stu, sizeof(stu));</span><br><span class="line"> <span class="built_in">return</span> infile;</span><br><span class="line">&#125;</span><br><span class="line">// 文件流的&lt;&lt;运算符重载函数</span><br><span class="line">fstream&amp; operator&lt;&lt;(fstream&amp; outfile, Student&amp; stu) &#123;</span><br><span class="line"> outfile.write((char*)&amp;stu, sizeof(stu));</span><br><span class="line"> <span class="built_in">return</span> outfile; &#125;</span><br><span class="line">int <span class="function"><span class="title">main</span></span>()</span><br><span class="line">&#123;</span><br><span class="line"> Student stu[3] = &#123; // 定义Student结构体数组并初始化</span><br><span class="line"> &#123;<span class="string">&quot;1210101&quot;</span>, <span class="string">&quot;张三&quot;</span>, 618&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210102&quot;</span>, <span class="string">&quot;李四&quot;</span>, 625&#125;,</span><br><span class="line"> &#123;<span class="string">&quot;1210103&quot;</span>, <span class="string">&quot;王五&quot;</span>, 612&#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> int i;</span><br><span class="line"> fstream outfile(<span class="string">&quot;studentinfo.dat&quot;</span>, ios::binary|ios::out); // 以写方式打开二进制文件</span><br><span class="line"> <span class="keyword">if</span> (!outfile.is_open()) // 判断文件打开操作是否成功</span><br><span class="line"> &#123;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;以写方式打开文本文件studentinfo.dat失败！&quot;</span>&lt;&lt;<span class="string">endl;</span></span><br><span class="line"><span class="string"> return 0;</span></span><br><span class="line"><span class="string"> &#125;</span></span><br><span class="line"><span class="string"> // 将第i名学生的数据写入文件，相当于调用outfile.operator&lt;&lt;(stu[i]);</span></span><br><span class="line"><span class="string"> for (i=0; i&lt;3; i++)</span></span><br><span class="line"><span class="string"> outfile&lt;&lt;stu[i];</span></span><br><span class="line"><span class="string"> outfile.close(); // 关闭文件</span></span><br><span class="line"><span class="string"> fstream infile(&quot;studentinfo.dat&quot;, ios::binary|ios::in); // 以读方式打开二进制文件</span></span><br><span class="line"><span class="string"> if (!infile.is_open()) // 判断文件打开操作是否成功</span></span><br><span class="line"><span class="string"> &#123;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;&quot;以读方式打开文本文件studentinfo.dat失败！&quot;&lt;&lt;endl</span>;</span><br><span class="line"> <span class="built_in">return</span> 0;</span><br><span class="line"> &#125;</span><br><span class="line"> // 将第i名学生的数据读入到内存中，相当于调用infile.operator&gt;&gt;(stu[i]);</span><br><span class="line"> <span class="keyword">for</span> (i=0; i&lt;3; i++)</span><br><span class="line"> infile&gt;&gt;stu[i];</span><br><span class="line"> <span class="keyword">for</span> (i=0; i&lt;3; i++)</span><br><span class="line"> &#123;</span><br><span class="line"> cout&lt;&lt;<span class="string">&quot;第&quot;</span>&lt;&lt;<span class="string">i+1&lt;&lt;&quot;名学生的学号、姓名和成绩：&quot;;</span></span><br><span class="line"><span class="string"> cout&lt;&lt;stu[i</span>]; // 相当于调用cout.operator&lt;&lt;(stu[i]);</span><br><span class="line"> &#125;</span><br><span class="line"> infile.close(); // 关闭文件</span><br><span class="line"> <span class="built_in">return</span> 0</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://leonlee233.github.io/2020/05/25/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E8%BF%90%E8%A1%8C%E5%AE%9E%E4%BE%8B/" data-id="clo6uzd8u00168cukajs2h8nn" data-title="输入输出运行实例" class="article-share-link"><span class="fa fa-share">Teilen</span></a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Hide/" rel="tag">Hide</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" rel="tag">学习心得</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/05/25/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%B5%81%E7%A8%8B%E5%BA%8F%E5%AE%9E%E4%BE%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          输入输出流程序实例
        
      </div>
    </a>
  
  
    <a href="/2020/05/24/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">输入输出基础知识点</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hide/" rel="tag">Hide</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Literature-Review/" rel="tag">Literature Review</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9B%9E%E5%BF%86%E5%BD%95/" rel="tag">回忆录</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%A5%87%E6%80%AA%E7%9A%84%E7%A2%8E%E7%A2%8E%E5%BF%B5/" rel="tag">奇怪的碎碎念</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" rel="tag">学习心得</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag">学习笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%91%84%E5%BD%B1/" rel="tag">摄影</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%99%E7%A8%8B/" rel="tag">教程</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B8%B8%E8%AE%B0/" rel="tag">游记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BE%8E%E5%9B%BE/" rel="tag">美图</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Hide/" style="font-size: 20px;">Hide</a> <a href="/tags/Literature-Review/" style="font-size: 10px;">Literature Review</a> <a href="/tags/%E5%9B%9E%E5%BF%86%E5%BD%95/" style="font-size: 12px;">回忆录</a> <a href="/tags/%E5%A5%87%E6%80%AA%E7%9A%84%E7%A2%8E%E7%A2%8E%E5%BF%B5/" style="font-size: 10px;">奇怪的碎碎念</a> <a href="/tags/%E5%AD%A6%E4%B9%A0%E5%BF%83%E5%BE%97/" style="font-size: 18px;">学习心得</a> <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" style="font-size: 10px;">学习笔记</a> <a href="/tags/%E6%91%84%E5%BD%B1/" style="font-size: 14px;">摄影</a> <a href="/tags/%E6%95%99%E7%A8%8B/" style="font-size: 16px;">教程</a> <a href="/tags/%E6%B8%B8%E8%AE%B0/" style="font-size: 12px;">游记</a> <a href="/tags/%E7%BE%8E%E5%9B%BE/" style="font-size: 10px;">美图</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/10/">October 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">November 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/10/26/DETR%E7%AC%94%E8%AE%B0/">文章总结：DETR笔记</a>
          </li>
        
          <li>
            <a href="/2023/07/14/%E4%B8%80%E4%B8%AA%E5%A5%BD%E7%9A%84%E5%BC%80%E5%A7%8B/">一个好的开始</a>
          </li>
        
          <li>
            <a href="/2021/11/20/linux%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%89%E8%A3%85/">linux系统的安装</a>
          </li>
        
          <li>
            <a href="/2021/11/20/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%9F%BA%E7%A1%80step1-hadoop/">大数据基础step1:hadoop</a>
          </li>
        
          <li>
            <a href="/2021/06/05/Linux%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">Linux学习笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 Leon Lee<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">主页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>